;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;作者:evilbinary on 11/19/16.
;邮箱:rootdebug@163.com
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(import  (scheme) (gles gles1)  (glut glut) (gui imgui)  )



(define (infix-prefix lst)
  (if (list? lst)
      (if (null? (cdr lst))
          (car lst)
          (list (cadr lst)
                (infix-prefix (car lst))
                (infix-prefix (cddr lst))))
      lst))



(define (app-calc)
  (let ((exp "") (clear #t))
    (glut-init)
    (imgui-init)
    (imgui-reset-style 7)
                                    ;(android)
                                        ;(imgui-scale (* 1.5 (android-get-density)) (* 1.5 (android-get-density)))
    (glut-touch-event (lambda (type x y)
                        (imgui-touch-event type x y)

                        ))
    (glut-mouse-event (lambda (button state)
                                        ;(glut-log "mouse-event")
                        (imgui-mouse-event button state)))
    (glut-motion-event (lambda (x y)
                                        ;(glut-log "motion-event")
                         (imgui-motion-event x y)
                         ))
    (glut-key-event (lambda (event)
                      (imgui-key-event
                       (glut-event-get event 'type)
                       (glut-event-get event 'keycode)
                       (glut-event-get event 'char)
                       (glut-event-get event 'chars))
                      (if (= 4 (glut-event-get event 'keycode ))
                          (begin (imgui-exit)
                                 (glut-exit)))
                      ))

    (glut-display (lambda ()
                    (imgui-render-start)
                                        ;(imgui-test)
                    (imgui-set-next-window-size (imgui-make-vec2 210.0 280.0) 1)
                    (imgui-begin "calculator" 0)
                    (imgui-text (format "exp: ~A" exp))
                    (imgui-separator)

                    (if (imgui-button "C" (imgui-make-vec2 88.0 40.0) )
                        (set! exp "")
                        (set! clear #t) 
                        )
                    ;;(imgui-same-line)
                    ;;(if (imgui-button "+/-" (imgui-make-vec2 80.0 40.0) )
                    ;;    (set! exp (string-append exp " + "))                        
                    ;;    )
                    (imgui-same-line)
                    (if (imgui-button "%" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp " % ")))
                    (imgui-same-line)
                    (if (imgui-button "÷" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp " / ")))

                    (if (imgui-button "7" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "7")))
                    
                    (imgui-same-line)
                    (if (imgui-button "8" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "8")))
                    
                    (imgui-same-line)
                    (if (imgui-button "9" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "9"))                        
                        )
                    (imgui-same-line)
                    (if (imgui-button "x" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp " * "))                                                
                        )

                    (if (imgui-button "4" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "4"))                                                
                        )
                    (imgui-same-line)
                    (if (imgui-button "5" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "5"))                                        
                        )
                    (imgui-same-line)
                    (if (imgui-button "6" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "6"))
                        )

                    (imgui-same-line)
                    (if (imgui-button "-" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp " - "))                        
                        )

                    (if (imgui-button "1" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "1"))                        
                        )
                    (imgui-same-line)
                    (if (imgui-button "2" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "2"))                        
                        )
                    (imgui-same-line)
                    (if (imgui-button "3" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "3"))                        
                        )
                    (imgui-same-line)
                    (if (imgui-button "+" (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp " + "))                        
                        )

                    (if (imgui-button "0" (imgui-make-vec2 88.0 40.0) )
                        (set! exp (string-append exp "0"))                        
                        )
                    (imgui-same-line)
                    (if (imgui-button "." (imgui-make-vec2 40.0 40.0) )
                        (set! exp (string-append exp "."))                        
                        )
                    
                    (imgui-same-line)
                    (if (and  (imgui-button "=" (imgui-make-vec2 40.0 40.0) ) clear (> (string-length exp) 0) )
                        ;;(glut-log (infix-prefix (read (open-input-string exp))))
                        (begin 
                            (set! exp (string-append exp (format " = ~a" (eval
                                                                          (infix-prefix
                                                                           (read (open-input-string
                                                                                  (string-append (string-append "(" exp) ")")))))))) 
                            (set! clear #f) )
                        
                        )

                    (imgui-end)
                    (imgui-render-end)
                    ))
    (glut-reshape (lambda(w h)
                    (imgui-resize w h)
                    ))
    (glut-main-loop)
    (imgui-exit)
    (glut-exit)
    ))
 (app-calc)
